package com.zmj.sy.mom.srv.aps.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmj.sy.mom.srv.aps.bean.entity.resource.LogContent;
import com.zmj.sy.mom.srv.aps.bean.entity.resource.LogSystem;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseIdReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BasePageResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.logSystem.LogSystemReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.logSystem.LogSystemResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.logapi.LogApiDetailResVo;
import com.zmj.sy.mom.srv.aps.mapper.LogContentMapper;
import com.zmj.sy.mom.srv.aps.mapper.LogSystemMapper;
import com.zmj.sy.mom.srv.aps.utils.SyExceptionUtils;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.List;
import java.util.Objects;

@RequiredArgsConstructor
@Service
public class LogSystemService {

    private final LogSystemMapper logSystemMapper;

    private final LogContentMapper logContentMapper;

    @SneakyThrows
    public BasePageResVo<LogSystemResVo> log(LogSystemReqVo reqVo) {

        Page<LogSystem> page = logSystemMapper.lambdaQuery()
                .like(StringUtils.hasText(reqVo.getApiName()), LogSystem::getApiName, reqVo.getApiName())
                .like(StringUtils.hasText(reqVo.getAppId()), LogSystem::getAppId, reqVo.getAppId())
                .like(StringUtils.hasText(reqVo.getQueryParam()), LogSystem::getQueryParam, reqVo.getQueryParam())

                .ge(StringUtils.hasText(reqVo.getStartTime()), LogSystem::getCreateDateTime, reqVo.getStartTime())
                .le(StringUtils.hasText(reqVo.getEndTime()), LogSystem::getCreateDateTime, reqVo.getEndTime())

                .orderByDesc(LogSystem::getCreateDateTime)
                .page(reqVo.toPage());

        return BasePageResVo.of(page, LogSystemResVo.class);
    }

    @SneakyThrows
    public LogApiDetailResVo detail(BaseIdReqVo reqVo) {

        LogSystem logSystem = logSystemMapper.selectById(reqVo.getId());

        if (!StringUtils.isEmpty(logSystem.getQueryParam())&& logSystem.getQueryParam().length()>=200) {
            LogContent logContent = logContentMapper.lambdaQuery().eq(LogContent::getLogId, logSystem.getId()).one();
            if(logContent!=null){
                LogApiDetailResVo res = new LogApiDetailResVo();
                res.setUrl(logSystem.getUrl());
                res.setRequestBody(logContent.getReqContent());
                res.setResponseBody(logContent.getResContent());
                return res;
            }

        }

       /* String requestPath = logSystem.getRequestPath();

        File file = new File(requestPath);
        if (!file.exists()) {
            throw SyExceptionUtils.e("未找到该日志的报文");
        }
        List<String> strings = FileUtils.readLines(file, "UTF-8");*/
        LogApiDetailResVo res = new LogApiDetailResVo();
        res.setUrl(logSystem.getUrl());
        res.setRequestBody(logSystem.getQueryParam());
        res.setResponseBody(logSystem.getResponsePath());

        return res;
    }


}
